package top.italks.manufactureproject.utils;

import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class HttpUtils {

    //    private static HttpUtils httpUtils;
    private static OkHttpClient okHttpClient;

    private static OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient();
        }
        return okHttpClient;
    }

//    public HttpUtils() {
//        okHttpClient = new OkHttpClient();
//    }
public static void post(String url, Callback callback) {
    post(url, null, callback);
}

    public static void post(String url, HashMap<String, Object> map, Callback callback) {
        FormBody.Builder builder = new FormBody.Builder();
        if (map != null) {
            if (map.size() > 0) {
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    builder.add(entry.getKey(), String.valueOf(entry.getValue()));
                }
            }
        }
        Request request = new Request.Builder()
            .url(url)
            .post(builder.build())
            .build();

        Call call = getOkHttpClient().newCall(request);
        call.enqueue(callback);
    }
}
